package com.atguigu.test1;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 11, 22, 33, 77, 99, 103};
        int target = 77;
        System.out.println(binarySearch(arr, target));
    }

    public static int binarySearch(int[] arraySorted, int target) {
        int left = 0;
        int right = arraySorted.length - 1;
        while (left <= right) {
            int middle = left + (right - left) / 2;
            if (arraySorted[middle] == target) {
                return middle;
            } else if (arraySorted[middle] > target) {
                right = middle - 1;

            } else {
                left = middle + 1;

            }
        }
        return -1;
    }
}
